package com.qwf.shici.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.qwf.shici.R;
import com.qwf.shici.base.BaseActivity;
import com.qwf.shici.base.ListBaseAdapter;
import com.qwf.shici.base.SuperViewHolder;
import com.qwf.shici.model.PoemBean;
import com.qwf.shici.support.utils.DataUtil;

import java.util.List;

/**
 * 推荐诗词列表页面
 */
public class PoemListActivity extends BaseActivity {
    private RecyclerView rcv_poem;//推荐诗词列表

    private List<PoemBean> poemBeanList;//诗词数据
    private PoemListAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_poem_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_poem = findViewById(R.id.rcv_poem);

        setHeadTitle("推荐诗词");

        poemBeanList = DataUtil.getPoemList();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_poem.setLayoutManager(manager);
        adapter = new PoemListAdapter(this);
        rcv_poem.setAdapter(adapter);
        adapter.setDataList(poemBeanList);

        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Intent intent = new Intent(PoemListActivity.this, PoemDetailActivity.class);
                intent.putExtra("poem", poemBeanList.get(position));
                startActivity(intent);
            }
        });

        setRightText("搜索");
    }

    @Override
    protected void onRightTextClick() {
        Intent intent = new Intent(this, SearchActivity.class);
        startActivity(intent);
    }

    private class PoemListAdapter extends ListBaseAdapter<PoemBean> {

        public PoemListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_poem_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_title = holder.getView(R.id.tv_title);
            TextView tv_author = holder.getView(R.id.tv_author);
            TextView tv_content = holder.getView(R.id.tv_content);

            PoemBean poem = mDataList.get(position);
            tv_title.setText("《" + poem.getTitle() + "》");
            tv_author.setText(poem.getAuthor());
            tv_content.setText(poem.getContent());
        }
    }
}
